package com.yilin.tms.system.components;

import com.yilin.commons.util.DateTimeUtil;
import com.yilin.tms.core.commons.captcha.CaptchaUtil;
import com.yilin.tms.core.commons.utils.OrderUtil;
import com.yilin.tms.core.commons.utils.SMSUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 易林云服务
 * 后台定时任务总
 * 系统定时任务分发控制中心
 */
@Service
public class SystemServiceTimerTask {

    Logger logger = LoggerFactory.getLogger(SystemServiceTimerTask.class);


    // 一般任务: 每3分钟执行一次，适用于自动过期任务等
    @Scheduled(fixedDelay = 3 * 60 * 1000)
    public void generalTaskTimer() {
        try {
            sendCodeExpireTask();
            captchaCodeExpireTask();
            captchaSlideExpireTask();
        } catch (Exception e) {
            logger.error("一般任务(3分钟)执行失败！" + e.getMessage());
        }
    }

    // 一般任务: 每10分钟执行一次，适用于登录失效
    @Scheduled(fixedDelay = 10 * 60 * 1000)
    public void logoutTaskTimer() {
        try {
            tokenAccessExpireTask();
        } catch (Exception e) {
            logger.error("一般任务(10分钟)执行失败！" + e.getMessage());
        }
    }

    // 异常处理任务: 每1小时执行一次，适用于失败重试等
    @Scheduled(fixedDelay = 60 * 60 * 1000)
    public void recoverTaskTimer() {
        try {

            //网络货运数据生成失败 重试
            //ETC自动订单数据生成失败 重试
            //...
        } catch (Exception e) {
            logger.error("异常处理任务(1h)执行失败！" + e.getMessage());
        }
    }


    //------------方法区------------------

    //>>> REDIS 自动任务区

    //货源推送凌晨重置任务
    private void goodsPushResetTask() {
        Map<String, Long> allGoodsPushMapTime = OrderUtil.getAllGoodsPushMapTime();
        allGoodsPushMapTime.forEach((fieldKey, aLong) -> {
            if (DateTimeUtil.secondBetween(aLong, System.currentTimeMillis()) > 24 * 60 * 60) {
                OrderUtil.removeSaveGoodsPush(fieldKey);
            }
        });
    }

    //验证码超过10分钟自动过期任务
    private void sendCodeExpireTask() {
        Map<String, Long> allSendCodeMapTime = SMSUtil.getAllSendCodeMapTime();
        allSendCodeMapTime.forEach((fieldKey, aLong) -> {
            if (DateTimeUtil.secondBetween(aLong, System.currentTimeMillis()) > 10 * 60) {
                SMSUtil.deleteSendCode(fieldKey);
            }
        });
    }

    //账户TOKEN30分钟不访问自动退出登录任务
    private void tokenAccessExpireTask() {
//        Map<String, Long> allTokenAccessTime = UserUtil.getAllTokenAccessTime();
//        allTokenAccessTime.forEach((fieldKey, aLong) -> {
//            if (DateTimeUtil.secondBetween(aLong,System.currentTimeMillis()) > 30*60){
//                UserUtil.removeLoginUser(fieldKey);
//            }
//        });
    }

    //图片验证码自动过期任务
    private void captchaCodeExpireTask() {
        Map<String, Long> allCaptchaCodeMapTime = CaptchaUtil.getAllCaptchaCodeMapTime();
        allCaptchaCodeMapTime.forEach((fieldKey, aLong) -> {
            if (DateTimeUtil.secondBetween(aLong, System.currentTimeMillis()) > 10 * 60) {
                CaptchaUtil.removeCaptchaCode(fieldKey);
            }
        });
    }

    //滑动验证码自动过期任务
    private void captchaSlideExpireTask() {
        Map<String, Long> allCaptchaSlideMapTime = CaptchaUtil.getAllCaptchaSlideMapTime();
        allCaptchaSlideMapTime.forEach((fieldKey, aLong) -> {
            if (DateTimeUtil.secondBetween(aLong, System.currentTimeMillis()) > 10 * 60) {
                CaptchaUtil.removeCaptchaSlide(fieldKey);
            }
        });
    }



}

